<?php
namespace app\special\service;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class Excel
{    /*数据匹配*/
    public static function matching($list=null)
    {
        $data = [
            ['id' => '1', 'title' => '产品名称', 'name' => "product_name"],
            ['id' => '2', 'title' => '产品名称（英文）', 'name' => "product_name_en"],
            ['id' => '3', 'title' => '公司集团', 'name' => "chinese_name"],
            ['id' => '4', 'title' => '生产企业', 'name' => "manufacturing_enterprise_name"],
            ['id' => '5', 'title' => '生产企业(英文)', 'name' => "manufacturing_enterprise_name_en"],
            ['id' => '6', 'title' => '商品名称', 'name' => "commodity_name_cn"],
            ['id' => '7', 'title' => '通用名称', 'name' => "common_name"],
            ['id' => '8', 'title' => '注册号', 'name' => "registration_number"],
            ['id' => '9', 'title' => '国产/进口', "name" => 'domestic_type'],
            ['id' => '10', 'title' => '剂型', "name" => 'dosage_form'],
            ['id' => '11', 'title' => '原产国', "name" => 'country_of_origin'],
            ['id' => '12', 'title' => '省份', "name" => 'areaname'],
            ['id' => '13', 'title' => '生产地址', "name" => 'production_address'],
            ['id' => '14', 'title' => '产品类别', "name" => 'product_category'],
            ['id' => '15', 'title' => '产品类型', "name" => 'product_type'],
            ['id' => '16', 'title' => '适用年龄', "name" => 'agename'],
            ['id' => '17', 'title' => '适用人群', "name" => 'target_audience'],
            ['id' => '18', 'title' => '食用方法/食用量', "name" => 'consumption'],
            ['id' => '19', 'title' => '口服/管饲', "name" => 'oral_type'],
            ['id' => '20', 'title' => '净含量和规格', "name" => 'net_content'],
            ['id' => '21', 'title' => '保质期', "name" => 'shelf_life'],
            ['id' => '22', 'title' => '配料表', "name" => 'original_ingredients'],
            ['id' => '23', 'title' => '营养成分表', "name" => '?'], ['id' => '24', 'title' => '营养成分（注）', "name" => 'nutrient_composition'],
            ['id' => '25', 'title' => '配方特点/营养学特征', "name" => 'formula_features'], ['id' => '26', 'title' => '渗透压', "name" => 'osmotic_pressure'],
            ['id' => '27', 'title' => '能量密度', "name" => 'production_address'], ['id' => '28', 'title' => '贮存条件', "name" => 'storage_conditions'],
            ['id' => '29', 'title' => '警示说明和注意事项', "name" => 'warning_specifications'],
            ['id' => '30', 'title' => '有效期', "name" => 'validity_period'],
            ['id' => '31', 'title' => '变更历史记录', "name" => 'change_history'],
            ['id' => '32', 'title' => '审批结论', "name" => 'approval_conclusion'],
            ['id' => '33', 'title' => '生产许可证', "name" => 'production_license'],
            ['id' => '34', 'title' => '发证机关（生产许可）', "name" => 'issuing_authority'],
            ['id' => '35', 'title' => '有效期(生产许可)', "name" => 'validity_production'],
            ['id' => '36', 'title' => '备注', "name" => 'remarks']
        ];
        if (!empty($list)) {
            $start = [];
            foreach ($list as $k => $v) {
                foreach ($data as $key => $val) {
                    if ($v['id'] == $val['id']) {
                        $start[] = $val;
                    }
                }
            }
            return $start;
        } else {
            return $data;
        }
    }

    public static function mystyle($headers=[],$list=[])
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $column = 'A';
        foreach ($headers as $h) {
            $sheet->setCellValue($column . '1', $h['title']);
            $column = chr(ord($column) + 1);
        }
        // 设置列宽
        // 设置列宽
        $column = 'A';
        foreach ($headers as $header) {
            $sheet->getColumnDimension($column)->setWidth(20);
            $column = chr(ord($column) + 1);
        }
        $column = chr(ord($column) - 1);
        // 设置行高
        $sheet->getRowDimension('1')->setRowHeight(30);
        // 设置颜色
        $styleArray = [
            'fill' => [
                'fillType' => Fill::FILL_SOLID,
                'color' => ['argb' => 'FF118868'],
            ],
            'font' => [
                'bold' => true,
            ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,
                'vertical' => Alignment::VERTICAL_CENTER,
            ],
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN,
                    'color' => ['argb' => 'FF000000'],
                ],
            ]
        ];
        $sheet->getStyle('A1:' . $column . '1')->applyFromArray($styleArray);
        $row = 2;

        foreach ($list as $item) {
            $column = 'A';
            foreach ($headers as $header) {
                $sheet->setCellValue($column . $row, $item[$header['name']]);
                $column++;
            }
            $row++;
        }
        return $spreadsheet;
    }
}